package 自定义异常;

/**
 * @ClassName: AgeException
 * @Auther: Drawin
 * @Date: 2021/10/27 15:58
 * @Description:
 */

public class AgeException extends Exception{
    public AgeException(){
        //调用父类的构造方法，将“年龄超过范围”赋值给父类的message变量
        super("年龄异常");//异常错误信息是固定的
    }
    public AgeException(String message){
        //调用父类的构造方法，将message传递给父类的message变量
        super(message);//异常错误信息由调用者指定
    }
}
    class Person{
        private int age;
        private String name;
        public String getName(){return name;}
        public void setName(String name){this.name=name;}
        public int getAge(){return age;}
        public void setAge(int age)throws AgeException{
            if(age<0||age>200)
                throw new AgeException();
            this.age=age;
        }
    }

